package com.smart_campus.campus.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.smart_campus.campus.vo.CampusMideaVo;
import com.smart_campus.sys.common.DataGridView;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.smart_campus.campus.domain.CampusMedia;
import com.smart_campus.campus.mapper.CampusMediaMapper;
import com.smart_campus.campus.service.CampusMediaService;

import java.util.List;

/**
 * @author Alfred
 * @time: 2020/6/29 14:25
 */
@Service
public class CampusMediaServiceImpl implements CampusMediaService {

    @Resource
    private CampusMediaMapper campusMediaMapper;

    //@CacheEvict 删除的注解通过对象id在redis中删除数据
    @CacheEvict(cacheNames = "com.smart_campus.campus.service.impl.CampusMediaServiceImpl",key = "#id")
    @Override
    public int deleteByPrimaryKey(Integer id) {
        return campusMediaMapper.deleteByPrimaryKey(id);
    }

    //@CachePut 要返回对象 添加通过reids主键回填的id来在redis创建对应id的对象数据
    @CachePut(cacheNames = "com.smart_campus.campus.service.impl.CampusMediaServiceImpl",key = "#result.id")
    @Override
    public CampusMedia insert(CampusMedia record) {
        campusMediaMapper.insert(record);
        return record;
    }

    //@CachePut 要返回对象 添加通过reids主键回填的id来在redis创建对应id的对象数据
    @CachePut(cacheNames = "com.smart_campus.campus.service.impl.CampusMediaServiceImpl",key = "#result.id")
    @Override
    public CampusMedia insertSelective(CampusMedia record) {
        campusMediaMapper.insertSelective(record);
        return record;
    }

    //@Cacheable 返回对象 通过对象id在redis中创建对象保存,方便下次使用
    @Cacheable(cacheNames ="com.smart_campus.campus.service.impl.CampusMediaServiceImpl",key = "#id")
    @Override
    public CampusMedia selectByPrimaryKey(Integer id) {
        return campusMediaMapper.selectByPrimaryKey(id);
    }

    //@CachePut 要返回对象 添加通过reids主键回填的id来在redis创建对应id的对象数据
//    @CachePut(cacheNames = "com.smart_campus.campus.service.impl.CampusMediaServiceImpl",key = "#result.id")
    @Override
    public CampusMedia updateByPrimaryKeySelective(CampusMedia record) {
        campusMediaMapper.updateByPrimaryKeySelective(record);
        return record;
    }

    //@CachePut 要返回对象 添加通过reids主键回填的id来在redis创建对应id的对象数据
//    @CachePut(cacheNames = "com.smart_campus.campus.service.impl.CampusMediaServiceImpl",key = "#result.id")
    @Override
    public CampusMedia updateByPrimaryKey(CampusMedia record) {
        campusMediaMapper.updateByPrimaryKey(record);
        return record;
    }

    @Override
    public DataGridView queryMidia(CampusMideaVo campusMidiaVo) {
        Page<Object> page = PageHelper.startPage(campusMidiaVo.getPage(), campusMidiaVo.getLimit());
        List<CampusMideaVo> campusMideaVos = this.campusMediaMapper.queryMidia(campusMidiaVo);
        return new DataGridView(page.getTotal(), campusMideaVos);
    }

    @Override
    public Integer queryMax() {
        return this.campusMediaMapper.queryMax();
    }

}

